package com.proton.carepatchtemp.utils.Ntf;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.media.RingtoneManager;
import android.net.Uri;

import com.proton.carepatchtemp.R;
import com.proton.carepatchtemp.application.VCare;

import static android.content.Context.NOTIFICATION_SERVICE;

/**
 * Created by MoonlightSW on 2017/1/12.
 */

public class NotificationUtils {
    private static NotificationManager mNotificationManager;
    private static Notification notification;

    /**
     * 创建通知，  * 请在调用此方法时开启子线程
     *
     * @param context    上下文
     * @param tickerText 通知未拉开的内容
     * @param title      通知标题
     * @param content    通知主内容
     * @param intent     意图
     */
    public static void createNotif(Context context, String tickerText, String title, String content, Intent intent, int id) {

        if (null == context) {
            return;
        }

        mNotificationManager = (NotificationManager)
                context.getSystemService(NOTIFICATION_SERVICE);
        Notification.Builder builder = new Notification.Builder(context);
        builder.setContentText(content);
        builder.setContentTitle(title);
        builder.setSmallIcon(R.mipmap.ic_launcher);
        builder.setTicker(tickerText);
        builder.setAutoCancel(true);
        builder.setWhen(System.currentTimeMillis());
        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
        builder.setContentIntent(pendingIntent);
        Uri ntfUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
        builder.setDefaults(Notification.DEFAULT_LIGHTS);
        if (null != ntfUri) {
            builder.setSound(ntfUri);
        } else {
            builder.setDefaults(Notification.DEFAULT_SOUND);
        }
        builder.setPriority(Notification.PRIORITY_HIGH);
        Notification notification = builder.build();
        notification.flags = Notification.FLAG_INSISTENT;
        mNotificationManager.notify(id, notification);
    }

    //删除通知
    public static void clearNotification(int ntfCode) {
        NotificationManager notificationManager = (NotificationManager)
                VCare.get().getSystemService(NOTIFICATION_SERVICE);
        notificationManager.cancel(ntfCode);
    }

}
